﻿using System.Collections.Generic;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel;
using VOICeVIO.Hybrid.Models;
using VOICeVIO.Hybrid.Services;

namespace VOICeVIO.Hybrid.ViewModels
{
    public class DefaultViewModel : MasterPageViewModel
    {
        public string SearchId { get; set; }
        private readonly HybridService _hybridService;

        public DefaultViewModel(HybridService hybridService)
        {
            this._hybridService = hybridService;
        }

        [Bind(Direction.ServerToClient)]
        public List<SongListModel> SongList { get; set; }

        public override async Task PreRender()
        {
            SongList = await _hybridService.GetAllPublicSongsAsync();
            await base.PreRender();
        }

        public async Task SearchForSong(string uid)
        {
            var song = await _hybridService.GetSongByUidAsync(uid);
            if (song == null)
            {
                return;
            }

            Context.RedirectToRoute("Download", new { Id = song.Uid, Name = song.SongName });
        }
    }
}
